<template>
  <DetailsFormBox :title="page.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      hidden: !isEdit,
      click: getForm,
    },
    {
      title: route.query.xieYiShuHao ? '保存' : '仅保存',
      icon: '',
      type: 'primary',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form, false)
      },
    },
    {
      title: '保存并确认',
      icon: '',
      type: 'primary',
      hidden: !isEdit,
      click: (form: any) => {
        saveBtn(form, true)
      },
    },
  ]" :loading="data.loading" :loadingMsg="data.loadingMsg">
    <template #default="{ form }">
      <div class="p-12">
        <OrderInfo :data="data.productOrder" :produceTaskBillNo="route.query.produceTaskBillNo" />
        <CellBox title="基本信息" isTop>
          <FlexBox>
            <ElFormItemBox label="领料单号" prop="getMaterialBillNo">
              <ElInputBox :data="form" dataName="getMaterialBillNo" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="创建人员" prop="createPersonName">
              <ElInputBox :data="form" dataName="createPersonName" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="创建日期" prop="createDate">
              <ElInputBox :data="form" dataName="createDate" placeholder="系统自动生成" disabled />
            </ElFormItemBox>
            <ElFormItemBox label="领料类型" prop="getMaterialType" isRules>
              <ElSelectBox :data="form" dataName="getMaterialType" :options="options.getMaterialType"
                :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="领料备注" prop="getMaterialRemark" class="wAuto">
              <ElInputBox :data="form" dataName="getMaterialRemark" type="textarea" :disabled="!isEdit" />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
        <CellBox title="单据明细" isTop>
          <GoodList :data="{
            disabled: !isEdit,
            height: 328,
            rowKey: 'specificationCode',
            prop: 'getProduceMaterialMxList',
            tbody: form.getProduceMaterialMxList,
            thead: [
              {
                label: '申请数量',
                prop: 'applyGetQuantity',
                formType: 'number',
                isRules: true,
                width: 140,
              },
              {
                label: '机台名称',
                prop: 'machineCode',
                formType: 'select',
                options: options.machine,
                labelName: 'machineName',
                valueName: 'machineCode',
                isRules: true,
                width: 140,
              },
              {
                label: '备注',
                prop: 'getMaterialRemark',
                formType: 'textarea'
              },
            ],
            operation: [
              {
                label: '删除',
                icon: 'Delete',
              }
            ]
          }" @selectBtn="(rowArr: any) => {
            const _arr = rowArr.map((el: any) => ({
              produceMaterialNo: '', // 生产物料代码
              stockGoodsCode: el.specificationCode, // 库存物料代码
              applyGetQuantity: null, // 申请领料数量
              getMaterialRemark: '', // 领料备注
              machineCode: '', // 机台编号
              goodsCode: el.goodsCode,// 物料编号
              supplierCode: el.supplierCode, // 供应商编码
              specificationCode: el.specificationCode // 物料规格编码
            }))
            form.getProduceMaterialMxList.push(..._arr)
          }">
          </GoodList>
        </CellBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { sendMsg } from '@/utils/crossTagMag.ts';
import { useListComponent } from '../pageTs'; // 引入逻辑
import page from '../page.ts'; // 引入逻辑
import '../pageCss.scss'; // 引入样式
const { dataInfo, options } = useListComponent();
const { proxy } = getCurrentInstance() as any;
const data: any = reactive({
  loading: false,
  loadingMsg: '',
  mainPage: 1,
  form: {
    createDate: '', // 创建日期
    createPersonName: '', // 创建人员
    getMaterialID: 0, // 生产领料主键ID
    getMaterialBillNo: "", // 生产领料单号
    produceTaskBillNo: "", // 生产任务单号
    saleOrderNo: "", // 销售订单号
    getMaterialType: 1, // 领料类型：1生产领料；2生产补料
    getMaterialPerson: "", // 领料人员
    getMaterialRemark: "", // 领料备注
    equipmentNo: "", // 使用设备编码
    getProduceMaterialMxList: [
      // {
      //   produceMaterialNo: "", // 生产物料代码
      //   stockGoodsCode: "", // 库存物料代码
      //   applyGetQuantity: 0.0, // 申请领料数量
      //   getMaterialRemark: "", // 领料备注
      //   machineCode: "", // 机台编号
      //   goodsCode: "", // 物料编号
      //   supplierCode: "", // 供应商编码
      //   specificationCode: "" // 物料规格编码
      // }
    ]
  },
});
const isEdit = ref(true)

import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
//获取数据
const getForm = async () => {
  let _query: any = route.query
  setLoading()
  //获取工单信息
  //是否新建
  if (!!!_query.getMaterialBillNo) {
    if (!!_query.produceTaskBillNo) {
      proxy.$api.ProductionBusniess.GetProduceTaskBillDetail({
        produceTaskBillNo: _query.produceTaskBillNo
      })
        .then((res: any) => {
          data.form.getProduceMaterialMxList = res.produceMaterialList.map((el: any) => ({
            goodsCategoryName: el.categoryName,
            applyGetQuantity: el.stockQuantity,
            machineCode: el.equipmentNo,
            specificationCode: el.stockGoodsCode,
            stockGoodsCode: el.stockGoodsCode,
          }))
          data.form.saleOrderNo = res.baseData.saleOrderGroupNumber//订单单号
          data.form.produceTaskBillNo = res.baseData.produceTaskBillNo//生产单号
          data.loading = false
        })
        .catch(() => {
        });
    } else {
      data.loading = false
    }
    return
  }
  proxy.$api.ProductionBusniess.GetProduceMaterialBillDetail({
    getMaterialBillNo: _query.getMaterialBillNo,
  })
    .then((res: any) => {
      proxy.$message('获取成功')
      isEdit.value = (['1', '2'].includes(_query.type) && res.baseData.getMaterialBillState === 0)
      data.form = res.baseData
      data.loading = false
    })
    .catch(() => {
      data.loading = false
    });
};
const saveBtn = (form: any, isConfirm: boolean = false) => {
  const _form: any = {
    getMaterialID: form.getMaterialID, // 生产领料主键ID
    getMaterialBillNo: form.getMaterialBillNo, // 生产领料单号
    produceTaskBillNo: form.produceTaskBillNo, // 生产任务单号
    saleOrderNo: form.saleOrderNo, // 销售订单号
    getMaterialType: form.getMaterialType, // 领料类型：1生产领料；2生产补料
    getMaterialPerson: form.getMaterialPerson, // 领料人员
    getMaterialRemark: form.getMaterialRemark, // 领料备注
    equipmentNo: '', // 使用设备编码(没使用)
    mxDataList: form.getProduceMaterialMxList.map((el: any) => (
      {
        produceMaterialNo: el.produceMaterialNo || '', // 生产物料代码
        stockGoodsCode: el.specificationCode, // 库存物料代码
        applyGetQuantity: el.applyGetQuantity, // 申请领料数量
        getMaterialRemark: el.getMaterialRemark, // 领料备注
        machineCode: el.machineCode, // 机台编号
        goodsCode: el.goodsCode, // 物料编号
        supplierCode: el.supplierCode, // 供应商编码
        specificationCode: el.specificationCode // 物料规格编码
      }
    ))
  }
  switch (route.query.type) {
    case '1':
      proxy.$api.ProductionBusniess.CreateGetProduceMaterialBill(_form)
        .then((res: any) => {
          if (isConfirm) {
            setState(res)
          } else {
            _sun()
          }
        })
        .catch(() => { });
      break;
    case '2':
      proxy.$api.ProductionBusniess.UpdateGetProduceMaterialBill(_form)
        .then((res: any) => {
          _form.dataBillID = res
          if (isConfirm) {
            setState(_form)
          } else {
            _sun()
          }
        })
        .catch(() => { });
      break;

    default:
      break;
  }

  function _sun() {
    closeBtn('保存成功')
  }
};
const setState = (row: any) => {
  proxy.$api.ProductionBusniess.SureGetProduceMaterialBill({
    getMaterialID: row.getMaterialID
  })
    .then((res: any) => {
      closeBtn()
    })
    .catch(() => { });
};
const closeBtn = (message?: string) => {
  if (message) {
    proxy.$message('保存成功')
  }
  sendMsg('save', true);
  // getForm()
  window.close();
};
const setLoading = (title: string = '正在获取数据,请稍等...') => {
  if (!data.loading) {
    data.loading = true;
    data.loadingMsg = title;
  }
};
import { getMachine } from '@/utils/optionsArr.ts';
onMounted(() => {
  getForm();
  getMachine(options, 'machine')//获取设备
});
</script>
